package com.doge.walknovel.business.reader.viewholders

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.doge.wnpro.R
import com.doge.walknovel.api.model.TextInfo
import com.doge.walknovel.core.base.LoadStatus
import com.doge.walknovel.core.base.adapters.AdapterCallback
import com.doge.wnpro.databinding.CellReaderTextBinding
import com.doge.walknovel.utils.getColor
import com.doge.walknovel.utils.getString
import com.doge.walknovel.utils.html

class TextViewHolder(
    parent: ViewGroup,
    callback: AdapterCallback,
    private val binding: CellReaderTextBinding = CellReaderTextBinding.inflate(
        LayoutInflater.from(parent.context), parent, false),
) : RecyclerView.ViewHolder(binding.root) {

    fun bind(item: TextInfo) {
        itemView.tag = item

        if (item.status == LoadStatus.FAILURE) {
            binding.text.text = String.format(
                getString(R.string.xxx_chapter_content_load_error),
                item.chapterId.toString()
            )
        } else if (item.status == LoadStatus.LOADING) {
            binding.text.text = getString(R.string.content_loading)
        } else {
            binding.text.text = item.text.html
        }

        binding.text.textSize = item.size.toFloat()
        binding.text.typeface = item.typeface
        binding.text.setTextColor(getColor(item.color))
    }
}